/**
 * \file webrequest.h
 *       Filename:  webrequest.h
 * =====================================================================================
 * 
 * 
 *    Description:  Definition of methods for HTTP web access. Since the algorithms are
 *    				able to import compressed data, an uncompression library is needed.
 *    				In this module, we use standard LibZ.
 * 
 *        Version:  1.0
 *        Created:  13/11/2009 00:07:44
 *       Revision:  none
 *       Compiler:  gcc
 * 
 *         Author:  François Hissel (fh), francois.hissel@m4x.org
 *        Company:  
 * 
 * =====================================================================================
 */

#ifndef  WEBREQUEST_H_INC
#define  WEBREQUEST_H_INC

//#define OSM_PREFIX "http://"
//#define OSM_HOST "www.setra.equipement.gouv.fr"
//#define OSM_EXPORT "/"
#define HTTP_PREFIX "http://"	//!< Prefix for connection to an HTTP web page
#define	HTTP_PORT	80			//!< Standard port of an HTTP server 
#define	HTTP_HEADERS	"User-Agent: Mozilla/5.0 Gecko/2009090216 Ubuntu/9.04 (jaunty) Firefox/3.0.14\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Encoding: gzip,deflate\r\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"			//!< Other headers of HTTP request
//#define	HTTP_HEADERS	"User-Agent: Mozilla/5.0 Gecko/2009090216 Ubuntu/9.04 (jaunty) Firefox/3.0.14\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\nAccept-Encoding: gzip,deflate\nAccept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\n"			//!< Other headers of HTTP request
#define	BUFFER_SIZE	0x10000			//!< Size of buffer for loading the headers of a HTTP server response. The user must make sure this value is greater than the real size of the headers, or the downloading function won't work.

extern char proxy[0x100];	//!< Name of proxy server 
extern int proxy_port;	//!< Port of proxy server

/**
 * \brief Look for a substring in a buffer
 *
 * This function looks for the indicated substring in the buffer starting at position buffer and of a length of num bytes. If the substring is not found, the function returns 0. Otherwise, it returns the starting position of the substring in the buffer.
 * \param buffer String in which the search will take place
 * \param num Number of bytes of the buffer, the search will not go beyond this limit
 * \param substring Substring to search in the buffer
 * \param case_sensitive If set to 0, search is not case-sensitive, that is two strings will be considered equal even if some characters do not have the same case. Otherwise, search is case-sensitive.
 * \return Pointer to the starting position of the substring in the buffer, 0 if the substring is not found
 */
char* strnstr(char *buffer,size_t num,char *substring,int case_sensitive); 

/**
 * \brief Downloads a file from the website which address is given in argument
 *
 * This function connects to the desired website, using an HTTP protocol, and downloads the file given in argument, saving it on disk. It checks the global variable proxy to find if a proxy should be used and changes the headers according to its value. The website must reference a file, all headers generated by web server or intermediate proxy will be discarded. Zlib and Gzip-compressed files (gzip, deflate) are supported and will be automatically extracted before being saved on the disk. The function returns the size of the downloaded file. If an error occurs during the reading, the function returns -1 and a message is printed on stderr.
 * \param fd Descriptor of the file on which the content of the website will be saved, the file must already be opened.
 * \param host Host name of the web server
 * \param port Port to use for the connection : the port must be open and accept TCP connections with HTTP protocols
 * \param path Path of the file on the server, which may include all necessary parameters
 * \param headers Headers of the HTTP request
 * \return Number of bytes of the newly-created file, which is the number of bytes downloaded from the web site (excluding the headers) if the file is not zipped, or the size of the extracted file otherwise
 */
unsigned long download(int fd,char *host,int port,char *path,char *headers);


#endif   // ----- #ifndef WEBREQUEST_H_INC  -----
